package com.backwaveweibo.bww.service.impl;

import com.backwaveweibo.bww.entity.User;
import com.backwaveweibo.bww.entity.Weibo;
import com.backwaveweibo.bww.mapper.WeiboMapper;
import com.backwaveweibo.bww.service.LikeService;
import com.backwaveweibo.bww.service.WeiboService;
import com.backwaveweibo.bww.utils.FileUploadWithQiNiuYun;
import com.backwaveweibo.bww.utils.JwtTokenUtil;
import com.qiniu.common.QiniuException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author GHW : RD-Kelsey
 * @description 微博-业务实现类
 * @date 2020/6/9
 */

@Service
@Slf4j
public class WeiboServiceImpl implements WeiboService {

    @Resource
    private WeiboMapper weiboMapper;
    @Resource
    private LikeService likeService;


    /**
     * 发布微博
     *
     * @param weibo 微博实体
     * @return 发布是否成功
     */
    @Override
    public boolean addWeibo(Weibo weibo) {
        //从token取userId
        Long userId = Objects.requireNonNull(JwtTokenUtil.getUser()).getUserId();
        //如果是转发的微博
        if (weibo.getForward() != null) {
            //把转发的原微博id传进来
            weibo.setForward(weibo.getForward());
            //如果有转发的情况下，内容为空，默认生成 "转发微博"
            if (weibo.getContent() == null || weibo.getContent().equals("")) {
                weibo.setContent("转发微博");
            }
        }
        if (weibo.getContent().equals("") || weibo.getContent() == null) {
            return false;
        }
        //生成时间并格式化
        //转String存储
        weibo.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return weiboMapper.addWeibo(weibo, userId);
    }

    /**
     * 按条件获取微博信息，可传入任意实体中的可接收参数，无参数则全查
     *
     * @param weibo 实体
     * @return list集合
     */
    @Override
    public List<Weibo> getWeiboInfoByCondition(Weibo weibo) {
        Long userId = Objects.requireNonNull(JwtTokenUtil.getUser()).getUserId();
        if (weibo == null) {
            weibo = new Weibo();
            weibo.setUser(new User().setUserId(userId));
        }
        return weiboMapper.getWeiboInfoByCondition(weibo);
    }

    /**
     * 删除微博，会删除七牛云上对应的图片
     *
     * @param weiboId 微博id
     * @return true | false 删除是否成功
     */
    @Override
    public boolean deleteWeibo(Long weiboId) throws QiniuException {
        //查询本地是否有对应的微博
        Weibo weibo = weiboMapper.queryById(weiboId);
        String[] single;
        //获取七牛云外链前缀
        String domain = FileUploadWithQiNiuYun.DOMAIN;
        if (weibo != null) {
            //获取数据库存储的图片链接，可能是多张，中间用英文逗号隔开
            String userfile = weibo.getUserfile();
            //如果没有图片，直接删除
            if (userfile == null || userfile.equals("")) {
                return weiboMapper.deleteWeibo(weiboId);
            }
            //分隔逗号，获取每张图片的链接
            single = userfile.split(",");
            for (String s : single) {
                //去除七牛云外链前缀
                String replaceAll = s.replaceAll(domain, "");
                //从七牛云中删除对应图片
                FileUploadWithQiNiuYun.delFromQiNiu(replaceAll);
            }
            return weiboMapper.deleteWeibo(weiboId);
        }
        return false;
    }

    /**
     * 查询所有用户发的微博
     *
     * @return 微博list集合
     */
    @Override
    public List<Weibo> queryAll() {
        List<Weibo> weiboList = weiboMapper.queryAll();
        //获取<所有用户>的微博<点赞数>
        for (Weibo w : weiboList) {
            w.setLikeCount(likeService.getLikeCount(w.getWeiboId()));
        }
        return weiboList;
    }

    /**
     * 得到当前用户的微博总条数
     *
     * @return 总条数
     */
    @Override
    public List<Weibo> countCurrentUserWeibo() {
        User user = JwtTokenUtil.getUser();
        return weiboMapper.countCurrentUserWeibo(Objects.requireNonNull(user).getUserId());
    }

    //    --------------------------------Elasticsearch-----------------------------------------------

    @Override
    public Weibo queryById(Long weiboId) {
        return weiboMapper.queryById(weiboId);
    }
}
